package com.xxxx.yeb.controller;

import com.xxxx.yeb.pojo.Admin;
import com.xxxx.yeb.pojo.ChatMsg;
import com.xxxx.yeb.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;
import java.util.Map;

@RestController
public class WebSocketController {

    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    //用来接收”/chat/admin”路径发送来的消息
    @GetMapping("/chat/admin")
    public List<Admin> showAllUsers(){
        return adminService.showAllUsers();
    }
    @MessageMapping("/ws/chat")
    public void chat(Principal principal, ChatMsg msg){
        msg.setFrom(principal.getName());
        simpMessagingTemplate.convertAndSendToUser(msg.getTo(),"/queue/chat",msg);
    }


}

